﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.IO;

using BOL.IO;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.IO
{
    public class GeneValueReader : IReader<Dictionary<int,double[]>>
    {
        #region Private variables

        private bool _isFirstRowHeader;
        private int _numberOfValues;
        private char[] _delimiters;
        private StreamReader _sr;

        #endregion

        #region Constructors

        public GeneValueReader(FileStream fs, bool isFirstRowHeader, int numberOfValues, char[] delimiters)
        {
            _sr = new StreamReader(fs);
            _isFirstRowHeader = isFirstRowHeader;
            _numberOfValues = numberOfValues;
            _delimiters = delimiters;
        }

        public GeneValueReader(string path, bool isFirstRowHeader, int numberOfValues, char[] delimiters) : this(new FileStream(path, FileMode.Open), isFirstRowHeader, numberOfValues, delimiters) { }

        #endregion

        #region Public methods

        public Dictionary<int, double[]> Read()
        {
            var line = String.Empty;
            string[] temp; // temporary string array splitted by delimiter

            int geneId;
            double[] values;
            var geneValues = new Dictionary<int,double[]>();

            if (_isFirstRowHeader)
                _sr.ReadLine();

            while (!_sr.EndOfStream)
            {
                line = _sr.ReadLine();
                temp = line.Split(_delimiters);
                values = new double[_numberOfValues];

                geneId = Int32.Parse(temp[0]);
                for (int i = 0; i < _numberOfValues; i++)
                    values[i] = Double.Parse(temp[i + 1]);

                geneValues.Add(geneId, values);
            }

            return geneValues;
        }

        public void Close()
        {
            _sr.Close();
        }

        #endregion
    }
}
